namespace SimpleWebServices.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class ContactCollectionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<ContactCollection>()
                .DerivesFrom<Collection<Contact>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("contacts")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            ContactCollection actual = (
                "<contacts>" +
                    "<contact key='foo' />" +
                    "<contact key='bar' />" +
                "</contacts>").Deserialize<ContactCollection>();

            Assert.NotNull(actual);

            Assert.NotEmpty(actual);
        }

        [Fact]
        public void deserializeEmpty()
        {
            ContactCollection actual = "<contacts />".Deserialize<ContactCollection>();

            Assert.NotNull(actual);

            Assert.Empty(actual);
        }

        [Fact]
        public void serialize()
        {
            ContactCollection obj = new ContactCollection
            {
                new Contact
                {
                    Key = "foo"
                },
                new Contact
                {
                    Key = "bar"
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("2=count(/contacts/contact)"));
            Assert.True((bool)navigator.Evaluate("1=count(/contacts/contact[@key='foo'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/contacts/contact[@key='bar'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new ContactCollection());
        }

        [Fact]
        public void op_indexer_string()
        {
            ContactCollection obj = new ContactCollection
            {
                new Contact
                {
                    Key = "foo"
                },
                new Contact
                {
                    Key = "bar"
                }
            };

            Assert.Same(obj[1], obj["bar"]);
        }

        [Fact]
        public void op_Contains_string()
        {
            ContactCollection obj = new ContactCollection
            {
                new Contact
                {
                    Key = "foo"
                }
            };

            Assert.True(obj.Contains("foo"));
            Assert.False(obj.Contains("bar"));
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new ContactCollection() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new ContactCollection() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new ContactCollection() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}